package com.woniuxy.microcontentserver.handler;

import com.woniuxy.microcontentserver.util.DateUtil;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 自定义转换器
 */
public class StringToDateConverter implements Converter<String, Date> {
    @Override
    public Date convert(String s) {

        Date date=null;
        try{
            if(StringUtils.hasLength(s)){
                //获得长度
                int lenth = s.length();
                if(lenth == 10){
                    date = DateUtil.str2Date(s,"yyyy-MM-dd");
                }else{
                    date = DateUtil.str2Date(s,"yyyy-MM-dd hh:mm:ss");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return date;
    }
}
